package com.sa07.ui.dialog;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import oracle.jdbc.OracleDriver;

import com.sa07.bean.DbConstant;
import com.sa07.config.OracleConfigInit;
import com.sa07.db.DbConfig;
import com.sa07.db.OracleSessionFactory;
import com.sa07.ui.bean.StatusNameConstant;
import com.sa07.ui.components.JstatusControl;
import com.sa07.ui.uitls.ControlContext;

/**
 * mesDialog设置
 * 
 * @author Long.shangbo
 * 
 *         2014-2-15 上午5:25:22
 */
public class MesConfigDialog extends DefaultConfigDialog {

	private static final long serialVersionUID = -3013782279224894852L;

	private int width = 370;

	private int height = 300;

	private JTextField dbIp = null;

	private JTextField dbName = null;

	private JTextField dbPort = null;

	private JTextField dbUserName = null;

	private JPasswordField dbUserPwd = null;

	private JButton testConnBtn = null;

	private JButton saveBtn = null;
	
	@Override
	protected void initListeners() {
		//测试连接
		testConnBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// 对界面上的值进行保存
				DbConfig dbCofig = setDbConfig();	 
				//对连接进行测试
				getOracleConnction(dbCofig);
			}
		});
		//进行保存
		saveBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
				// 对界面上的值进行保存
				boolean flag = getProperties(setDbConfig());
				if(flag){
					Object[] options = {"确认"};
					JOptionPane.showOptionDialog(null, "文件保存成功", "提示", 
					JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
					null, options, options[0]);
				}else{
					Object[] options = {"确认"};
					JOptionPane.showOptionDialog(null, "文件保存失败", "提示", 
					JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
					null, options, options[0]);
				}
			}
		});
	}

	@Override
	protected void initComponents() {
		int y = 20;
		int lblWidth = 100;
		int comHeight = 25;
		int marginLeft = 30;
		int txtWidth = 200;
		int lblX = 20;
		int txtX = lblWidth + marginLeft;
		int marginTop = 40;

		JLabel ipLbl = new JLabel("数据库服务器：");
		ipLbl.setBounds(lblX, y, lblWidth, comHeight);
		add(ipLbl);

		dbIp = new JTextField();
		dbIp.setBounds(txtX, y, txtWidth, comHeight);
		add(dbIp);

		y += marginTop;

		JLabel portLbl = new JLabel("数据库端口：");
		portLbl.setBounds(lblX, y, lblWidth, comHeight);
		add(portLbl);

		dbPort = new JTextField();
		dbPort.setBounds(txtX, y, txtWidth, comHeight);
		add(dbPort);

		y += marginTop;

		JLabel nameLbl = new JLabel("数据库名称：");
		nameLbl.setBounds(lblX, y, lblWidth, comHeight);
		add(nameLbl);

		dbName = new JTextField();
		dbName.setBounds(txtX, y, txtWidth, comHeight);
		add(dbName);

		y += marginTop;

		JLabel userNameLbl = new JLabel("数据库用户名：");
		userNameLbl.setBounds(lblX, y, lblWidth, comHeight);
		add(userNameLbl);

		dbUserName = new JTextField();
		dbUserName.setBounds(txtX, y, txtWidth, comHeight);
		add(dbUserName);

		y += marginTop;

		JLabel userPwdLbl = new JLabel("数据库密码：");
		userPwdLbl.setBounds(lblX, y, lblWidth, comHeight);
		add(userPwdLbl);

		dbUserPwd = new JPasswordField();
		dbUserPwd.setBounds(txtX, y, txtWidth, comHeight);
		add(dbUserPwd);

		int testBtnWidth = 100;
		int saveBtnWidth = 70;
		int btnMarginLeft = 30;
		y += marginTop;

		testConnBtn = new JButton("测试连接");
		testConnBtn.setBounds(txtX, y, testBtnWidth, comHeight);
		add(testConnBtn);

		saveBtn = new JButton("保存");
		saveBtn.setBounds(txtX + btnMarginLeft + testBtnWidth, y, saveBtnWidth,
				comHeight);
		add(saveBtn);
	}

	@Override
	protected int getWindowWidth() {
		return width;
	}

	@Override
	protected int getWindowHeight() {
		return height;
	}

	@Override
	protected String getWindowTitle() {
		return "Mes参数设置";
	}

	@Override
	protected void initValue() {
		
		OracleConfigInit init = new OracleConfigInit();
		init.init();
		
		DbConfig dbConfig = OracleSessionFactory.getFactory().getDbConfig();
		
		dbIp.setText(dbConfig.getDbIp());
		dbName.setText(dbConfig.getDbName());
		dbPort.setText(dbConfig.getDbPort() + "");
		dbUserName.setText(dbConfig.getDbUserName());
		dbUserPwd.setText(dbConfig.getDbUserPwd());
	}
	/**
	 *把界面上的值设置到DbConfig 
	 */
	private DbConfig setDbConfig(){
		// DbConfig 设置值
		DbConfig db = new DbConfig();
		// 获取ip 地址
		String getdbIp = dbIp.getText().trim();
		//获取数据库的名称
		String  getdbName = dbName.getText().trim();
        // 获取端口
		String  getdbPort = dbPort.getText().trim();
        // 数据库的用户名
		String  getdbUserName = dbUserName.getText().trim();
        // 获取用户的密码
		String  getdbUserPwd = dbUserPwd.getText().trim();
		db.setDbIp(getdbIp);
		db.setDbName(getdbName);
		db.setDbPort(Integer.parseInt(getdbPort));
		db.setDbUserName(getdbUserName);
		db.setDbUserPwd(getdbUserPwd);	
		return db;
//		getProperties(db);
	}
	/**
	 * 获取oracle 数据库连接,因为每次都要建立一个新的的连接
	 */
	public void getOracleConnction(DbConfig dbConfig){
		String URL = String.format("jdbc:oracle:thin:%s/%s@%s:%d:%s",
				dbConfig.getDbUserName(), dbConfig.getDbUserPwd(),
				dbConfig.getDbIp(), dbConfig.getDbPort(),
				dbConfig.getDbName());
		boolean mesState = false;
		try {
			DriverManager.registerDriver(new OracleDriver());
			Connection conn = DriverManager.getConnection(URL);
			if(conn != null){
				mesState = true;
				}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("oracel数据库连接失败");
		}
		
		if(mesState){
			Object[] options = {"确认"};
			JOptionPane.showOptionDialog(null, "数据库连接成功", "提示", 
			JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
			null, options, options[0]);
		}else{
			Object[] options = {"确认"};
			JOptionPane.showOptionDialog(null, "数据库连接失败", "提示", 
			JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
			null, options, options[0]);
		}
	}
	/**
	 * 加载属性文件,进行替换,并保存属性文件
	 */
	public boolean getProperties(DbConfig config){
	    Properties pro=new Properties(); 
	    try
		{
			InputStream in= new FileInputStream(new File("config/oracle_config.properties").getAbsoluteFile());
			pro.load(in);
			in.close();
			
			pro.setProperty(DbConstant.DB_IP,config.getDbIp() );
			pro.setProperty(DbConstant.DB_NAME,config.getDbName() );
			pro.setProperty(DbConstant.DB_PORT,config.getDbPort()+"" );
			pro.setProperty(DbConstant.DB_USERNAME,config.getDbUserName() );
			pro.setProperty(DbConstant.DB_USERPWD,config.getDbUserPwd() );
            FileOutputStream out = new FileOutputStream(new File("config/oracle_config.properties").getAbsoluteFile());
            pro.store(out,"");
            out.close();
            return true;
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			return false;
			
		}
	}
	/**
	 * 测试的类
	 * @param args
	 */
	public static void main(String[] args)
	{
//		MesConfigDialog m = new MesConfigDialog();
//		m.getProperties();
	}

}
